/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.android.apps.mytracks.io.maps;

import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
import com.google.android.apps.mytracks.io.sendtogoogle.UploadResultActivity;

import android.accounts.Account;
import android.app.Instrumentation;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;

/**
 * Tests the {@link SendMapsActivity}.
 * 
 * @author Youtao Liu
 */
public class SendMapsActivityTest extends ActivityInstrumentationTestCase2<SendMapsActivity> {

  SendMapsActivity sendMapsActivity;
  Instrumentation instrumentation;
  SendRequest sendRequest;

  public SendMapsActivityTest() {
    super(SendMapsActivity.class);
  }

  /**
   * Creates a class to override some methods of {@link SendMapsActivity} to
   * makes it testable.
   * 
   * @author youtaol
   */
  public class SendMapsActivityMock extends SendMapsActivity {
    /**
     * Ignores NullPointerException when new a intent in
     * {@link SendMapsActivity#startNextActivity(boolean, boolean)} by
     * overriding this method.
     */
    @Override
    protected void startNextActivity(boolean success, boolean isCancel) {
      try {
        super.startNextActivity(success, isCancel);
      } catch (NullPointerException e) {}
    }

    /**
     * By overriding this method, avoids to start an activity.
     */
    @Override
    public void startActivity(Intent intent) {}

    public void setSendRequest(SendRequest sendRequest) {
      super.sendRequest = sendRequest;
    }
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    sendRequest = new SendRequest(1, true, false, true);
    sendRequest.setAccount(new Account("testAccountName", Constants.ACCOUNT_TYPE));
  }

  /**
   * Tests the method {@link SendMapsActivity#CreateAsyncTask}.
   */
  public void testCreateAsyncTask() {
    Intent intent = new Intent();
    setActivityIntent(intent.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest));
    sendMapsActivity = this.getActivity();
    instrumentation = getInstrumentation();
    SendMapsAsyncTask sendMapsAsyncTask = (SendMapsAsyncTask) sendMapsActivity.createAsyncTask();
    assertNotNull(sendMapsAsyncTask);
  }

  /**
   * Tests the method {@link SendMapsActivity#startNextActivity}. Sets the
   * values of two parameter to true and true. Sets the flags to send fusion
   * table and send docs to false and true.
   */
  public void testStartNextActivity_success_cancel_sendDocs() {
    SendMapsActivityMock sendMapsActivityMock = new SendMapsActivityMock();
    sendRequest.setSendFusionTables(false);
    sendRequest.setSendDocs(true);
    sendMapsActivityMock.setSendRequest(sendRequest);
    sendMapsActivityMock.startNextActivity(true, true);
    assertEquals(true, sendRequest.isMapsSuccess());
    assertEquals(UploadResultActivity.class, sendMapsActivityMock.next);
  }

  /**
   * Tests the method {@link SendMapsActivity#startNextActivity}. Sets the
   * values of two parameter to true and false. Sets the flags to send fusion
   * table and send docs to true and false.
   */
  public void testStartNextActivity_success_notCancel_sendFusionTable() {
    SendMapsActivityMock sendMapsActivityMock = new SendMapsActivityMock();
    sendRequest.setSendFusionTables(true);
    sendRequest.setSendDocs(false);
    sendMapsActivityMock.setSendRequest(sendRequest);
    sendMapsActivityMock.startNextActivity(true, false);
    assertEquals(true, sendRequest.isMapsSuccess());
    assertEquals(SendFusionTablesActivity.class, sendMapsActivityMock.next);
  }

  /**
   * Tests the method {@link SendMapsActivity#startNextActivity}. Sets the
   * values of two parameter to false and false. Sets the flags to send fusion
   * table and send docs to false and false.
   */
  public void testStartNextActivity_failed_notCancel_sendNone() {
    SendMapsActivityMock sendMapsActivityMock = new SendMapsActivityMock();
    sendRequest.setSendFusionTables(false);
    sendRequest.setSendDocs(false);
    sendMapsActivityMock.setSendRequest(sendRequest);
    sendMapsActivityMock.startNextActivity(false, false);
    assertEquals(false, sendRequest.isMapsSuccess());
    assertEquals(UploadResultActivity.class, sendMapsActivityMock.next);
  }

  /**
   * Tests the method {@link SendMapsActivity#startNextActivity}. Sets the
   * values of two parameter to true and false. Sets the flags to send fusion
   * table and send docs to true and true.
   */
  public void testStartNextActivity_failed_notCancel_sendBoth() {
    SendMapsActivityMock sendMapsActivityMock = new SendMapsActivityMock();
    sendRequest.setSendFusionTables(true);
    sendRequest.setSendDocs(true);
    sendMapsActivityMock.setSendRequest(sendRequest);
    sendMapsActivityMock.startNextActivity(true, false);
    assertEquals(true, sendRequest.isMapsSuccess());
    // Sends fusion table at first.
    assertEquals(SendFusionTablesActivity.class, sendMapsActivityMock.next);
  }
}
